<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/4/7
 * Time: 14:28
 */
namespace App\Middleware;

use Asf\Contracts\Middleware\Middleware;
use Asf\Http\JsonResponse;

class CurrentLimit implements Middleware
{
    public function handle($request, $redis)
    {
        $num = $redis->get('apinum');
        if (empty($num)){
            $redis->set('apinum', 0);
            $redis->expire('apinum', 60);
        }

        if ($num < 10){
            $redis->incr('apinum');
            return true;
        }
        return new JsonResponse([
            'msg' => '短时间内访问过多，请稍后访问',
        ]);

    }

}